#pragma once
#include <windows.h>

template <class Child>
class Library
{
public:
	static char* Name;
	static DWORD Offset;

	static DWORD LoadDiabloLibrary()
	{
		DWORD proposedOffset = (DWORD)GetModuleHandle(Name);
		if(proposedOffset==NULL)
			proposedOffset = (DWORD)LoadLibrary(Name);
		if (proposedOffset == NULL)
		{
			exit(-1);
		}
		return proposedOffset;
	}

	// Retrieves the address by adding the DLLs base offset to the recorded offset
	static DWORD GetAddress(const DWORD offset)
	{
		DWORD proposedOffset = Offset + offset;
		//D2Log::log_msg("Address by Addition: (%08X + %08X @ %08X)\n", Offset, offset, proposedOffset);
		return proposedOffset;
	}

	static DWORD GetAddressByOrdinal(const DWORD ordinal)
	{
		HMODULE module = (HMODULE)Offset;
		DWORD locatedAddress = (DWORD)GetProcAddress(module, (LPCSTR)ordinal);
		//D2Log::log_msg("Address by Ordinal: (%08X / %08X @ %08X)\n", Offset, ordinal, locatedAddress);
		return locatedAddress;
	}
};

template <class Child>
char* Library<Child>::Name;

template <class Child>
DWORD Library<Child>::Offset;